package com.putao.album.tag.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.putao.album.R;
import com.putao.album.base.PtBaseAdapter;
import com.putao.album.tag.bean.TagAlbumItem;
import com.putao.album.tag.view.TopRoundedBitmapDisplayer;
import com.putao.album.util.StringHelper;

import java.util.List;

/**
 * Created by jidongdong on 15/4/28.
 */
public class TagAblumAdapter extends PtBaseAdapter {
    DisplayImageOptions options;
    private int itemWidth = 0;

    public TagAblumAdapter(Context context, List<TagAlbumItem> datalist) {
        super(context, datalist);
        options = new DisplayImageOptions.Builder()
                .considerExifParams(true)
//                .showStubImage(R.drawable.ic_stub)          // 设置图片下载期间显示的图片
//                .showImageForEmptyUri(R.drawable.ic_empty)  // 设置图片Uri为空或是错误的时候显示的图片
//                .showImageOnFail(R.drawable.ic_error)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new TopRoundedBitmapDisplayer(12))  // 设置成圆角图片
                .build();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        if (position == mDatalist.size() - 1) {
//            convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_tag_album_item, null);
//            ViewHolder viewHolder = new ViewHolder();
//            viewHolder.iv_left = (ImageView) convertView.findViewById(R.id.iv_left);
//            viewHolder.iv_right = (ImageView) convertView.findViewById(R.id.iv_right);
//            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
//            viewHolder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
//            TagAlbumItem item = (TagAlbumItem) getItem(position);
//            convertView.setBackgroundResource(item.Type == TagAlbumItem.DataType.NORMAL ? R.drawable.tag_album_item_bg_photo : R.drawable.tag_album_stroke_line_bg);
//            setData(viewHolder, item);
//            return convertView;
//        } else {
        ViewHolder viewHolder;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_tag_album_item, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_left = (ImageView) convertView.findViewById(R.id.iv_left);
            viewHolder.iv_right = (ImageView) convertView.findViewById(R.id.iv_right);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TagAlbumItem item = (TagAlbumItem) getItem(position);
        convertView.setBackgroundResource(item.Type == TagAlbumItem.DataType.NORMAL ? R.drawable.tag_album_item_bg_photo : R.drawable.tag_album_stroke_line_bg);
        setData(viewHolder, item);
        return convertView;
//        }

    }

    private void setData(final ViewHolder viewHolder, TagAlbumItem item) {
//        viewHolder.iv_left.clearAnimation();
//        viewHolder.iv_right.clearAnimation();
        if (item.Type == TagAlbumItem.DataType.NORMAL) {
            viewHolder.tv_name.setText(item.album_name);
            int count = !StringHelper.isEmpty(item.count) ? Integer.valueOf(item.count) : 0;
            viewHolder.tv_count.setText(count > 999 ? "999+" : item.count);
            if (count > 0) {
//                if (item.cover_urls != null) {
//                    if (item.cover_urls.length > 0) {
//                        viewHolder.setImagesUrl(item.cover_urls);
//                        ImageLoader.getInstance().displayImage(item.cover_url, viewHolder.iv_right, options);
////                        ImageLoader.getInstance().displayImage(item.cover_url, viewHolder.iv_left, options);
////                        ImageLoader.getInstance().displayImage(item.cover_url, viewHolder.iv_right, options);
////                        viewHolder.execAnimator(viewHolder.iv_left, viewHolder.iv_right);
//                    }
//                } else {
                if (!StringHelper.isEmpty(item.cover_url)) {
                    ImageLoader.getInstance().displayImage(item.cover_url, viewHolder.iv_right, options);
                }
//                }
            } else {
                viewHolder.iv_right.setImageResource(R.drawable.baby_icon_nophoto2);
            }
        } else {

            viewHolder.tv_name.setText(R.string.tag_add_new_title);
            viewHolder.tv_count.setText("");
            viewHolder.iv_right.setImageResource(R.drawable.tips_photo_tag22);
        }
    }

    public void setItemWidth(int width) {
        itemWidth = width;
    }

    public int getItemWidth() {
        return itemWidth;
    }


    class ViewHolder {
        public ImageView iv_left, iv_right;
        public TextView tv_name;
        public TextView tv_count;
        private String[] urls;

        public void setImagesUrl(String[] urls) {
            this.urls = urls;
        }

        void execAnimator(final ImageView out, final ImageView in) {
            in.postDelayed(new Runnable() {
                @Override
                public void run() {
                    TranslateAnimation animationOut = new TranslateAnimation(0, itemWidth, 0, 0);
                    animationOut.setDuration(1000);
                    out.startAnimation(animationOut);
                    TranslateAnimation animationIn = new TranslateAnimation(-itemWidth, 0, 0, 0);
                    animationIn.setDuration(1000);
                    in.startAnimation(animationIn);
                    animationIn.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            ImageLoader.getInstance().displayImage(urls[((int) (Math.random() * urls.length))], out, options, new ImageLoadingListener() {
                                @Override
                                public void onLoadingStarted(String imageUri, View view) {

                                }

                                @Override
                                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                                }

                                @Override
                                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                                    execAnimator(in, out);
                                }

                                @Override
                                public void onLoadingCancelled(String imageUri, View view) {

                                }
                            });

                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                }
            }, 2000 + (int) (Math.random() * 5 * 1000));
        }
    }
}
